﻿using Autofac;
using AVCUMPlatform.BLL.Implement;
using AVCUMPlatform.BLL.Interface;

namespace AVCUMPlatform.BLL
{
    public class BLLContainer
    {
        /// <summary>
        /// IOC 容器
        /// </summary>
        public static IContainer container = null;

        /// <summary>
        /// 获取 IDal 的实例化对象
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <returns></returns>
        public static T1 Resolve<T1>()
        {
            try
            {
                if (container == null)
                {
                    Initialise();
                }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("IOC实例化出错!" + ex.Message);
            }

            return container.Resolve<T1>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialise()
        {
            var builder = new ContainerBuilder();
            //格式：builder.RegisterType<xxxx>().As<Ixxxx>().InstancePerLifetimeScope();
            builder.RegisterType<MessageBLL>().As<IMessageBLL>().InstancePerLifetimeScope();
            builder.RegisterType<ClientInfoBLL>().As<IClientInfoBLL>().InstancePerLifetimeScope();
            builder.RegisterType<SourceBLL>().As<ISourceBLL>().InstancePerLifetimeScope();
            builder.RegisterType<MessageTypesBLL>().As<IMessageTypesBLL>().InstancePerLifetimeScope();
            builder.RegisterType<SystemInfoBLL>().As<ISystemInfoBLL>().InstancePerLifetimeScope();
            builder.RegisterType<MessageTmplatesBLL>().As<IMessageTmplatesBLL>().InstancePerLifetimeScope();
            
            container = builder.Build();
        }
    }
}
